package com.net.xpay.stat.scheduler;


import com.net.xpay.stat.task.CompanyDailyStatDailyTask;
import com.net.common.util.LocalDateUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.stat.CompanyDailyStat;
import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.common.helper.RedisHelper;
import com.net.xpay.common.service.CompanyDailyStatService;
import com.net.xpay.core.constant.RedisKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**

 * 后台经营数据日统计
 */
@Slf4j
@Component
public class CompanyDailyStatScheduler {

    @Autowired
    private CompanyDailyStatDailyTask companyDailyStatDailyTask;

    @Autowired
    private NoticeHelper noticeHelper;

    @Autowired
    private CompanyDailyStatService companyDailyStatService;

    @Autowired
    private RedisHelper redisHelper;

    /**
     * 更新后台经营数据日统计
     */
    @Scheduled(cron = "0 5 0 * * ?")
    public void doCompanyDailyStat() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule doCompanyDailyStat|now={}", LocalDateTime.now());
            companyDailyStatDailyTask.startDailyWork();
            long t2 = System.currentTimeMillis();
            log.info("end schedule doCompanyDailyStat|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("后台经营数据日统计生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("doCompanyDailyStat error", e);
            noticeHelper.sendSystemMonitorNotice("后台经营数据日统计失败|doCompanyDailyStat error", "text");
        }
    }

    /**
     * 跑一条
     */
    @Scheduled(cron = "0 40 3 * * ?")
    public void doHistoryCompanyDailyStat() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule doHistoryCompanyDailyStat|now={}", LocalDateTime.now());
            CompanyDailyStat monthStat = companyDailyStatService.statCompanyDailyStatByTime(LocalDateUtil.getMonthStartDay(LocalDateTime.now()), LocalDateUtil.getDayEnd(LocalDateTime.now().minusDays(1L)));
            redisHelper.set(RedisKey.COMPANY_MONTH_DATA_STAT, SerializationUtil.obj2String(monthStat));

            CompanyDailyStat companyDailyStat = companyDailyStatService.statCompanyDailyStatByTime(null, LocalDateUtil.getDayEnd(LocalDateTime.now().minusDays(1L)));
            redisHelper.set(RedisKey.COMPANY_HISTORY_DATA_STAT, SerializationUtil.obj2String(companyDailyStat));
            long t2 = System.currentTimeMillis();
            log.info("end schedule doHistoryCompanyDailyStat|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("后台经营历史总量数据统计生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("doHistoryCompanyDailyStat error", e);
            noticeHelper.sendSystemMonitorNotice("后台经营历史总量数据统计失败|doHistoryCompanyDailyStat error", "text");
        }
    }









}
